package org.lewis.woman.controllers;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.lewis.common.domain.Song;
import org.lewis.common.domain.User;
import org.lewis.common.service.SongService;
import org.lewis.common.service.TagService;
import org.lewis.common.utils.WebUtils;

import com.tocersoft.framework.core.annotations.Controller;
import com.tocersoft.framework.core.annotations.Injection;
import com.tocersoft.framework.core.cache.OSCacheManager;
import com.tocersoft.framework.core.utils.PropertiesUtils;
import com.tocersoft.framework.web.HttpUtils;
import com.tocersoft.framework.web.annotations.Mapping;
import com.tocersoft.framework.web.annotations.NameSpace;
import com.tocersoft.framework.web.renderer.Renderer;
import com.tocersoft.framework.web.renderer.TemplateRenderer;

/**
 * 添加歌曲
 * 
 * @author taiqichao
 * 
 */
@Controller
@NameSpace("/song/add")
public class SongAddcontroller {

	private final static String VP = "/WEB-INF/views/music/";

	Log log = LogFactory.getLog(SongAddcontroller.class);

	OSCacheManager cacheMang = OSCacheManager.getInstance();

	@Injection
	private TagService tagService;

	@Injection
	private SongService songService;

	/**
	 * 添加音乐
	 * 
	 * @return
	 * @throws Exception
	 */
	@SuppressWarnings("unchecked")
	@Mapping("")
	public Renderer add() throws Exception {

		if (WebUtils.getCurrentUser() == null) {
			WebUtils.getResponse().sendRedirect(HttpUtils.getContentPath() + "/accounts/login");
			return null;
		}

		Map<String, Object> data = new HashMap<String, Object>();

		// 检查用户上传限制时间
		String lcachekey = "u_limit_" + WebUtils.getCurrentUser().getId();

		int limitTime = Integer.parseInt(PropertiesUtils.getValue("user.song.limit.time"));

		Long uid = (Long) cacheMang.getCache(lcachekey, limitTime);

		if (uid != null) {
			initdata(data);
			data.put("msg", PropertiesUtils.getValue("user.song.limit.msg"));
			return new TemplateRenderer(VP + "add_song.vm", data);
		}

		if (!WebUtils.isPost()) {
			initdata(data);
			return new TemplateRenderer(VP + "add_song.vm", data);
		} else {

			String name = WebUtils.getParameter("form_name");
			String desc = WebUtils.getParameter("form_desc");
			String tags = WebUtils.getParameter("form_tags");
			String songFile = WebUtils.getParameter("form_file");
			String clientTokenId = WebUtils.getParameter("tokenid");
			String sessionTokenId = (String) WebUtils.getSession().getAttribute("tokenid");
			
			// 校验tokenid
			if(StringUtils.isBlank(clientTokenId)||StringUtils.isBlank(sessionTokenId)){
				initdata(data);
				return new TemplateRenderer(VP + "add_song.vm", data);
			}
			
			if (!StringUtils.equals(clientTokenId, sessionTokenId)) {
				initdata(data);
				return new TemplateRenderer(VP + "add_song.vm", data);
			}
			
			// 判断文件是否上传
			if (StringUtils.isBlank(songFile)) {
				log.debug("没有上传文件");
				data.put("msg", "请先添加声音");
				initdata(data);
				return new TemplateRenderer(VP + "add_song.vm", data);
			}
			
			
			//文件信息校验
			if (StringUtils.isBlank(name) || StringUtils.isBlank(desc)) {
				data.put("msg", "请将歌曲信息填写完整");
				initdata(data);
				return new TemplateRenderer(VP + "add_song.vm", data);
			}

			// 持久化歌曲信息
			Song song = new Song();
			song.setCreateTime(new Date());
			song.setSdesc(WebUtils.safe(desc));
			song.setName(WebUtils.safe(name));
			song.setTags(tags);
			song.setFile(songFile);
			
			User user=WebUtils.getCurrentUser();
			song.setUid(user.getId());
			song.setNickName(user.getName());
			
			long sid = songService.addSong(song);
			if (sid > 0) {
				// 加入缓存
				cacheMang
						.putCache(lcachekey, WebUtils.getCurrentUser().getId());
				data.put("success", "推荐成功");
				initdata(data);
				return new TemplateRenderer(VP + "add_song.vm", data);
			} else {
				data.put("msg", "推荐失败");
				initdata(data);
				return new TemplateRenderer(VP + "add_song.vm", data);
			}

		}

	}

	private void initdata(Map<String, Object> data) {
		// 生成tokenid
		WebUtils.getSession().setAttribute("tokenid",
				UUID.randomUUID().toString());
		data.put("tags0", tagService.queryByType(0));
		data.put("tags1", tagService.queryByType(1));
		data.put("tags2", tagService.queryByType(2));
	}

}
